$(function () {
    let types = eval("("+localStorage.getItem("types")+")")
    console.log(types,1111)
    let options=""
    for(let i of types)
        options+=`<option data-classId=${i.classId}>${i.className}</option>`
    $('#novel-classes').empty().append(options)
    $.ajax({
        url:"/novel",
        type:"get",
        success:function (data) {
            let list = data.NovelIdList
            $('.addNovel').change(function () {
                $('.addNovel').each(function () {
                    console.log(111)
                    if($(this).val()===""||$(this).val().trim().length===0){
                        $(this).parent().addClass("has-error")
                        $('#btnAdd').attr("disabled","disabled")
                    }
                    else {
                        $(this).parent().removeClass("has-error")
                        $('#btnAdd').removeAttr("disabled")
                    }

                })
            })
            $('#id').change(function () {
                for(let  i of list) {
                    if (i.id === $(this).val()) {
                        $('.info').text("小说编号已存在!!!")
                        $(this).parent().addClass("has-error")
                        $('#btnAdd').attr("disabled","disabled")
                        break;
                    }
                    else {
                        $('.info').empty()
                        $(this).parent().removeClass("has-error")
                        $('#btnAdd').removeAttr("disabled")
                    }
                }

            })
            $("#btnAdd").click(function () {
                let sel = $('#novel-classes option:selected ')
                let url = '/novel?id='+ $('#id').val()+
                            '&classId='+ $(sel).attr("data-classId")+
                            '&name='+$('#name').val()+
                            '&author='+$('#author').val()+
                            '&desc='+$('#desc').val()+
                            '&chap='+$('#chap').val()
                console.log(url)
                $.ajax({
                    url:url,
                    type:"PUT",
                    success:function (data) {
                        console.log(data)
                        location.href='index.html'
                    }
                })
            })

        }
    })
})